package com.lbs.droiddoctor.disease;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lbs.droiddoctor.R;
import com.lbs.droiddoctor.base.BaseActivity;
import com.lbs.droiddoctor.common.HttpASync;
import com.lbs.droiddoctor.common.HttpASync.OnRequest;
import com.lbs.droiddoctor.constants.AppConstants;
import com.lbs.view.adapter.SymptomsAdapter;

public class DiseaseSymptoms extends BaseActivity implements OnRequest{
	private ListView symptomsList;
	private Context ctx;
	private String disease;
	private ArrayList<Symptom> symptoms;
	private SymptomsAdapter adapter;
	private TextView textView;
	private Handler uiHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			

		}
	};

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.diseasesymptom);
		symptomsList = (ListView) findViewById(R.id.listView1);
		textView = (TextView ) findViewById(R.id.textView2);
		ctx=this;
		disease = getIntent().getStringExtra("DISEASE");
		textView.setText("Symptoms for "+disease);
		
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		String decodedDisease = "";
		try {
			decodedDisease =  URLEncoder.encode(disease, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String getSymptomPath = AppConstants.GET_SYMPTOM_PATH+"?disease="+decodedDisease;
		HttpASync request = new HttpASync();
		request.setOnRequestListener(this);
		request.execute(getSymptomPath);
		showProgress("Loading...");
	}
	@Override
	public void onResponseReceived(String response) {
		// TODO Auto-generated method stub
		Gson gson = new Gson();
		symptoms = gson.fromJson(response,
				new TypeToken<List<Symptom>>() {
				}.getType());
		runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				hideProgress();
				if(symptoms!=null ){
					ArrayList<String> symArr = new ArrayList<String>();
					for(int i=0;i<symptoms.size();i++) {
						symArr.add(symptoms.get(i).getSymptomName());
					}
					adapter = new SymptomsAdapter(ctx, symArr, uiHandler);
					adapter.setChangeIcon(true);
					symptomsList.setAdapter(adapter);
				}

			}
		});
		
	}

}
